/*******************************************************************************
 * Copyright (c) 2006, 2007 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * Contributors:
 * IBM Corporation - initial API and implementation
 *******************************************************************************/

package cn.elwy.rcp.ui.navigator.internal.resources.workbench;

import org.eclipse.core.resources.IFile;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.ide.ResourceUtil;
import org.eclipse.ui.navigator.ILinkHelper;
import org.eclipse.ui.part.FileEditorInput;

/**
 * Links IFileEditorInput to IFiles, and vice versa.
 * 
 * @since 3.2
 */
public class ResourceLinkHelper implements ILinkHelper {

  /*
   * (non-Javadoc)
   * @see org.eclipse.ui.navigator.ILinkHelper#findSelection(org.eclipse.ui.IEditorInput)
   */
  public IStructuredSelection findSelection(IEditorInput anInput) {
    IFile file = ResourceUtil.getFile(anInput);
    if (file != null) {
      return new StructuredSelection(file);
    }
    return StructuredSelection.EMPTY;
  }

  /*
   * (non-Javadoc)
   * @see org.eclipse.ui.navigator.ILinkHelper#activateEditor(org.eclipse.ui.IWorkbenchPage,
   * org.eclipse.jface.viewers.IStructuredSelection)
   */
  public void activateEditor(IWorkbenchPage aPage, IStructuredSelection aSelection) {
    if (aSelection == null || aSelection.isEmpty())
      return;
    if (aSelection.getFirstElement() instanceof IFile) {
      IEditorInput fileInput = new FileEditorInput((IFile) aSelection.getFirstElement());
      IEditorPart editor = null;
      if ((editor = aPage.findEditor(fileInput)) != null)
        aPage.bringToTop(editor);
    }

  }

}
